﻿using CMS.DataAccess;
using CMS.Excep;
using CMS.Model;

namespace CMS.AOP
{
    public class UserSaveAction : IBeforeSaveOrDelAction<UserModel>
    {
        public void DoAction(UserModel? _old, UserModel? _new)
        {
            if (_old != null && _new != null && _old.Pk == _new.Pk && _old.UserCode != _new.UserCode)
            {
                throw new MyArgException("登陆号不允许修改");
            }
            DbQuery<UserModel> dbQuery = new DbQuery<UserModel>();
            var duplicate = dbQuery.Where(CondCollection.True()
                .Eq(nameof(UserModel.UserCode), _new.UserCode)
                .NoEq(nameof(UserModel.Pk), _new.Pk))
                .First();
            if(duplicate!=null)
                throw new MyArgException("该登录号已经存在");
        }
    }
}
